#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root)
    {

        //return dfs(root->right,root->val) + dfs(root->left,root->val);
        return dfs(root, 0);
    }
    int dfs(TreeNode* root, int nums)
    {
        if (root == nullptr) return 0;
        int now_nums = nums * 10 + root->val;
        if (root->right == nullptr && root->left == nullptr)return now_nums;
        //int now_nums = nums*10+root->val;
        //dfs(root->left,now_nums);
        return dfs(root->right, now_nums) + dfs(root->left, now_nums);
    }
};